package com.leilei.controller;

import com.leilei.entity.CommodityColor;
import com.leilei.entity.CommodityVersion;
import com.leilei.service.ICommodityColorService;
import com.leilei.service.impl.CommodityColorServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/CommodityColorServlet")
public class CommodityColorServlet extends BaseServlet{

    private ICommodityColorService commodityColorService = new CommodityColorServiceImpl();

    public CommodityColorServlet(){

    }

    protected void getCommodityColorByPcid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int pcid = Integer.parseInt(req.getParameter("pcid"));

        CommodityColor commodityColor = commodityColorService.selectCommodityColorByPcid(pcid);

        req.setAttribute("commodityColor", commodityColor);

        req.getRequestDispatcher("jsp/updateCommodityColor.jsp").forward(req, resp);
    }

    protected void saveCommodityColor(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int pid = Integer.parseInt(req.getParameter("pid"));
        String colorname = req.getParameter("colorname");

        CommodityColor commodityColor = new CommodityColor();
        commodityColor.setPid(pid);
        commodityColor.setColorname(colorname);

        boolean result = commodityColorService.insertCommodityColor(commodityColor);
        if (result){
            //跳转页面
            req.getRequestDispatcher("/CommodityManagerServlet?method=getCommodityByPage&type=1").forward(req, resp);
        } else{
            //新增页面
            req.getRequestDispatcher("/jsp/insertCommodityColor.jsp?pid="+pid).forward(req, resp);
        }

    }

    protected void deleteCommodityColorByPcid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String spcid = req.getParameter("pcid");

        int pcid = spcid == null ? 0 : Integer.parseInt(spcid);

        boolean result = commodityColorService.deleteCommodityColor(pcid);
        if (result){
            //跳转页面

        } else{
            //新增页面

        }

    }

    protected void updateCommodityColor(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int pcid = Integer.parseInt(req.getParameter("pcid"));
        String colorname = req.getParameter("colorname");

        CommodityColor commodityColor = new CommodityColor();
        commodityColor.setPcid(pcid);
        commodityColor.setColorname(colorname);

        boolean result = commodityColorService.updateCommodityColor(commodityColor);
        if (result){
            //跳转页面
            req.getRequestDispatcher("/CommodityManagerServlet?method=getCommodityByPage&type=1").forward(req, resp);
        } else{
            //新增页面
            req.getRequestDispatcher("/jsp/updateCommodityColor.jsp?pid="+pcid).forward(req, resp);
        }

    }

}
